package hhh.app.presenter.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import hhh.app.R;
import hhh.app.data.bean.Review;

/**
 * Created by Administrator on 2017/2/28 0028.
 */
public class PinLunView extends LinearLayout {
    private Button btnShow;
    private List<View> views;
    private int showItems;
    private TextView title;
    private Button button;
    private OnClickListener listener;

    public class Data{

    }

    public PinLunView(Context context) {
        super(context);
        init(null);
    }

    public PinLunView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public PinLunView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs){
        setOrientation(VERTICAL);
        TypedArray a=getContext().obtainStyledAttributes(attrs, R.styleable.PinLunView);
        int N=a.getIndexCount();
        for(int i=0;i<N;i++){
            int index=a.getIndex(i);
            switch (index){
                case R.styleable.PinLunView_num:
                    showItems =a.getInteger(index,0);
                    break;
            }
        }
        a.recycle();
    }

    public void setData(List<Review> reviews){
        if(reviews==null){
            return;
        }
        removeAllViews();

        title=new TextView(getContext());
        title.setText("评论");
        this.addView(title, LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);

        int count=Math.min(reviews.size(),showItems);
        for(int i=0;i<count;i++){
            Review review=reviews.get(i);
            View view= LayoutInflater.from(getContext()).inflate(R.layout.layout_review_item,this,false);
            TextView ri_name= (TextView) view.findViewById(R.id.ri_name);
            TextView ri_content= (TextView) view.findViewById(R.id.ri_content);
            TextView ri_date= (TextView) view.findViewById(R.id.ri_date);
            ri_name.setText(String.valueOf(review.getCustomer_id()));
            ri_content.setText(review.getText());

            SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
            ri_date.setText(s.format(review.getDate_added()));
            this.addView(view,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        }

        button=new Button(getContext());
        button.setText("查看全部");
        this.addView(button,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        if(listener!=null){
            button.setOnClickListener(listener);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    public void setClickListener(OnClickListener listener){
        this.listener=listener;
    }
}
